#! /usr/bin/python

### AniGrid ##################
### http://www.anigrid.com ###
### s.eichberger@gmail.com ###

# system imports
import wx
import feedparser
#import MySQLdb

# AniGrid Imports
import AGdb
from AGrss import *
from AGdialogs import *
from AGwatch import *
from AGoptions import *

# main AniGrid Frame

class AGFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        ### Menu start
        menubar = wx.MenuBar()
        file = wx.Menu()
        edit = wx.Menu()
        help = wx.Menu()
        file.Append(101, '&Open', 'Open a new document')
        file.Append(102, '&Save', 'Save the document')
        file.AppendSeparator()
        quit = wx.MenuItem(file, 105, '&Quit\tCtrl+Q', 'Quit the Application')
    #    quit.SetBitmap(wx.Image('stock_exit-16.png',wx.BITMAP_TYPE_PNG).ConvertToBitmap())
        file.AppendItem(quit)
        edit.Append(201, 'check item1', '', wx.ITEM_CHECK)
        edit.Append(202, 'check item2', kind=wx.ITEM_CHECK)
        submenu = wx.Menu()
        submenu.Append(301, 'radio item1', kind=wx.ITEM_RADIO)
        submenu.Append(302, 'radio item2', kind=wx.ITEM_RADIO)
        submenu.Append(303, 'radio item3', kind=wx.ITEM_RADIO)
        edit.AppendMenu(203, 'submenu', submenu)
        help.Append(401, '&About', 'Provides general info')

        menubar.Append(file, '&File')
        menubar.Append(edit, '&Edit')
        menubar.Append(help, '&Help')
        
        ### menu binds

        self.Bind(wx.EVT_MENU, self.OnQuit, id=105)
        self.Bind(wx.EVT_MENU, self.OnAbout, id=401)
        
        ### Statusbar

        self.statusbar = self.CreateStatusBar()

        self.SetMenuBar(menubar)

        ### toolbar

        toolbar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL | wx.NO_BORDER)
        toolbar.AddSimpleTool(1001, wx.Image('images/watch_icon.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Watch Tab', 'Toggle Watch Tab',1)
        toolbar.AddSimpleTool(1002, wx.Image('images/rss_icon.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'RSS Tab', 'Toggle RSS Tab',1)
        toolbar.AddSeparator()
        toolbar.AddSimpleTool(1003, wx.Image('images/options_icon.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'NBOptions', 'Toggle Options Tab',1)
        toolbar.AddSeparator()
        toolbar.AddSimpleTool(1010, wx.Image('images/exit_icon.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap(), 'Quit', 'Quit')
        toolbar.Realize()

        self.Bind(wx.EVT_TOOL, self.OnNBWatch, id=1001)
        self.Bind(wx.EVT_TOOL, self.OnNBRss, id=1002)
        self.Bind(wx.EVT_TOOL, self.OnNBOptions, id=1003)
        self.Bind(wx.EVT_TOOL, self.OnQuit, id=1010)
                    
        # vbox for toolbar on top and tabs below
        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(toolbar,0,wx.EXPAND)

        # image

        logo = wx.Image('images/anigrid.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
#
# bitmap upper left corner is in the position tuple (x, y) = (5, 5)
#
     

        ### Notebook Tabs

        nbpanel = wx.Panel(self)
        self.nb = wx.Notebook(nbpanel,0)
        wx.StaticBitmap(nbpanel, -1, logo, (50, 20), (logo.GetWidth(),logo.GetHeight()))

        # add nbpanel below toolbar
        vbox.Add(nbpanel,1,wx.EXPAND)
        self.SetSizer(vbox)

        self.page_watch = PWatch(self.nb)
        self.page_rss = PRSS(self.nb)
        self.page_options = POptions(self.nb)

        nbsizer = wx.BoxSizer()
        nbsizer.Add(self.nb, 1, wx.EXPAND)
        nbpanel.SetSizer(nbsizer)

        ### Notebook Tabs End
        self.Centre()

        ### def's 

        ### menubar defs
    def OnQuit(self, event):
        self.Close()
    
    def OnAbout(self, event):
        aboutdia = AGAbout(self, -1,'')
        aboutdia.ShowModal()
        aboutdia.Destroy()

        ### menu end

        ### toolbar defs

    def OnNBWatch(self, event):
        tab = -1
        for i in range(self.nb.GetPageCount()):
            if ( self.nb.GetPageText(i) == "Watch" ):
                tab = i

        if( tab != -1 ):
            self.nb.RemovePage(tab)
        else:
            self.nb.AddPage(self.page_watch,"Watch")

    def OnNBRss(self, event):
        tab = -1
        for i in range(self.nb.GetPageCount()):
            if ( self.nb.GetPageText(i) == "RSS" ):
                tab = i

        if( tab != -1 ):
            self.nb.RemovePage(tab)
        else:
            self.nb.AddPage(self.page_rss,"RSS")

    def OnNBOptions(self, event):
        tab = -1
        for i in range(self.nb.GetPageCount()):
            if ( self.nb.GetPageText(i) == "Options" ):
                tab = i

        if( tab != -1 ):
            self.nb.RemovePage(tab)
        else:
            self.nb.AddPage(self.page_options,"Options")


# AGFrame end

# AniGrid Main Class 
class AniGrid(wx.App):
    def OnInit(self):
        frame = AGFrame(None, -1, 'AniGrid')
        frame.SetSize(wx.Size(1000,800))
        frame.Center()
        frame.Show(True)
        return True

# AniGrid end


# Start AniGrid
AG = AniGrid(0)
AG.MainLoop()
            
